package com.liuceng.zen.web.controller;

import com.liuceng.zen.pojo.domain.LandingPage;
import com.liuceng.zen.service.LandingPageFormService;
import com.liuceng.zen.service.LandingPageService;
import com.liuceng.zen.web.Page;
import com.liuceng.zen.web.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yanwen on 16-10-31.
 * LandingPageController :
 */
@Controller
public class LandingPageController extends BaseController{
    @Autowired
    private LandingPageService landingPageService;
    @Autowired
    private LandingPageFormService landingPageFormService;

    /**
     * 获取落地页列表 页面
     * @return
     */
    @RequestMapping(value = "/landing_pages/list", method = RequestMethod.GET)
    public String landingPage(ModelMap map) {
        //landingPageService.findPage();
        map.put("memberId",getCurrentUserId());
        map.put("diyFormDomain",landingPageService.getDiyFormDomain());
        return "landingPages/list";
    }

    /**
     *  获取页面数据
     * @return
     */
    @RequestMapping(value = "/ajax/landing_pages/pages", method = RequestMethod.GET)
    public @ResponseBody Object findPage(Pageable pageable) {
        pageable.setShowType(LandingPage.SHOW_TYPE);
        pageable.setMemberId(getCurrentUserId());
        Page<LandingPage> page=landingPageService.findPage(pageable);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("rows", page.getContent());
        pageMap.put("total", page.getTotal());
        pageMap.put("offset", pageable.getOffset());//当前页
        return pageMap;
    }

    /**
     * 获取添加页面
     * @param map
     * @return
     */
    @RequestMapping(value = "/ajax/landing_pages/add", method = RequestMethod.GET)
    public String landingPageAdd(ModelMap map) {
        map.put("memberId",getCurrentUserId());
        return "landingPages/_add";
    }
    /**
     * 获取广告添加页面
     * @param map
     * @return
     */
    @RequestMapping(value = "/ajax/get/landing_pages/add", method = RequestMethod.GET)
    public String getLandingPageAdd(ModelMap map) {
        map.put("memberId",getCurrentUserId());
        return "/ad/add_landingPages";
    }

    /**
     * 获取添加页面
     * @return
     */
    @RequestMapping(value = "/landing_pages/add", method = RequestMethod.POST)
    public @ResponseBody Object add(LandingPage landingPage) {
        landingPageService.createLandingPage(landingPage,getUserContext().getMember());
        return landingPage.getId();
    }

    /**
     * 获取修改页面
     * @param landingPageId
     * @param map
     * @return
     */
    @RequestMapping(value = "/landing_pages/edit/{landingPageId}", method = RequestMethod.GET)
    public String landingPageEdit(@PathVariable  Integer landingPageId, ModelMap map) {
        map.put("landingPageId",landingPageId);
        map.put("diyFormDomain",landingPageService.getDiyFormDomain());
        map.put("mainDomain",landingPageService.getMainDomain());
        return "landingPages/edit";
    }

    /**
     * 删除
     * @param landingPageId
     * @return
     */
    @RequestMapping(value = "/landing_pages/delete/{landingPageId}", method = RequestMethod.POST)
    public @ResponseBody Object landingPageDelete(@PathVariable  Integer landingPageId) {
        int count=landingPageService.getUseCount(landingPageId,getCurrentUserId(),LandingPage.SHOW_TYPE);
        if(count==0) {
            landingPageService.delete(landingPageId);
            landingPageFormService.deleteByLandingPage(landingPageId);
            return "success";
        }
        return "fails";
    }

    /**
     * 验证落地页名称是否唯一
     * @return
     */
    @RequestMapping(value = "/ajax/landing_pages/check_name", method = RequestMethod.GET)
    public @ResponseBody Object getLandingPageByName(String name,Integer showType) {
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("memberId",getCurrentUserId());
        map.put("showType",showType);
        LandingPage landingPage=landingPageService.findOneByParams(map);
        if(landingPage==null){
            return "success";
        }
        return "fails";
    }

}
